package com.controller;

import com.entity.Users;
import com.entity.table.UsersTableDef;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.UsersService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @GetMapping("/login")
    public String toLoginPage(){
        return "back/login";
    }

    @Autowired
    UsersService usersService;

    @PostMapping("/toLogin")
    public String login(Users users, HttpSession httpSession, Model model){

        QueryWrapper query = QueryWrapper.create()
                .where(UsersTableDef.USERS.USERNAME.eq(users.getUsername()))
                .and(UsersTableDef.USERS.PASSWORD.eq(users.getPassword()));
        Users users1 = usersService.getOne(query);
        if(users1!=null){
            httpSession.setAttribute("admin",users1);
            model.addAttribute("admin", users1);
            return "back/index";
        }
        else {
            model.addAttribute("msg","账号或密码错误，请重新输入！");
            return "back/login";
        }
    }
}
